package com.train.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.train.common.ServerResponse;
import com.train.pojo.User;
import com.train.service.IUserService;
import com.train.utils.GetOpenId;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/user/")
public class UserController {
	
	@Autowired
	private IUserService userService;

	//用户的登录功能,用户已存在,登录成功,用户不存在,添加用户后登录
	@RequestMapping("addUser")
	@ResponseBody
	public ServerResponse addUser(String jscode,String userInfo){
		
		String openid = GetOpenId.getOpenid(jscode);
		User user = new User();
		JSONObject jsonUser = JSONObject.fromObject(userInfo);
		String nickName = jsonUser.getString("nickName");
		user.setUserNickname(nickName);
		user.setUserAvatorUrl(jsonUser.getString("avatarUrl"));
		return userService.addUser(user,openid);
	}
}
